package kr.hkit.powerjava.alone.service;

import java.util.Scanner;

import kr.hkit.powerjava.alone.dao.BankInterfaceDao;

public abstract class ServiceItem<T> {
	protected BankInterfaceDao<T> dao;
	protected Scanner sc;

	public ServiceItem(BankInterfaceDao<T> dao, Scanner sc) {
		this.dao = dao;
		this.sc = sc;
		init();
		prnMenu();
	}

	public abstract T inputItemInfo(boolean gubun);

	public abstract void prnSubMenu();

	protected void init() {
		dao.loadFile();

	}

	protected void close() {
		dao.saveFile();
	}

	private void prnMenu() {
		int res = 0;
		T item = null;

		do {
			System.out.print("1.입력\n2.삭제\n3.수정\n4.검색\n5.리스트보기\n6. 종료\n");
			res = sc.nextInt();
			switch (res) {
			case 1:
				item = inputItemInfo(true);
				dao.insertItem(item);
				break;
			case 2:
				dao.prnItemList(null);
				System.out.println("삭제할 정보 입력");
				item = inputItemInfo(false);
				dao.deleteItem(item);
				break;
			case 3:
				dao.prnItemList(null);
				item = inputItemInfo(true);
				dao.updateItem(item);
				break;
			case 4:
				item = inputItemInfo(false);
				try {
					item = dao.searchItem(item);
					System.out.println(item);
				} catch (ArrayIndexOutOfBoundsException e) {
					System.out.println("해당 정보 음슴");
				}
				break;
			case 5:
				prnSubMenu();
				break;
			default:
				close();
				res = 7;

			}
		} while (res < 6);

	}
}
